package com.lovo.project.service.impl;

import com.lovo.project.dao.UserRoleDAO;
import com.lovo.project.enums.UserRoleCodeEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.RoleDO;
import com.lovo.project.service.UserRoleService;
import com.lovo.project.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @title 用户角色业务实现
 * @author: yuyuan
 * @date: 2020/11/3 13:57
 */
public class UserRoleServiceImpl implements UserRoleService {

    @Override
    public List<Integer> findRoleByUserId(Integer userId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        UserRoleDAO userRoleDao = sqlSession.getMapper(UserRoleDAO.class);
        List<Integer> roleIdList = userRoleDao.selectRoleByUserId(userId);
        sqlSession.close();
        return roleIdList;
    }

    @Override
    public List<RoleDO> findOtherRoleByUserId(Integer userId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        UserRoleDAO userRoleDao = sqlSession.getMapper(UserRoleDAO.class);
        List<RoleDO> roleList = userRoleDao.selectOtherRoleByUserId(userId);
        sqlSession.close();
        return roleList;
    }

    @Override
    public List<RoleDO> findAllRole() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        UserRoleDAO userRoleDao = sqlSession.getMapper(UserRoleDAO.class);
        List<RoleDO> list = userRoleDao.selectAllRole();
        sqlSession.close();
        return list;
    }

    @Override
    public void deleteRoleByUserIdAndRoleId(Integer userId, List<String> roleIdDeleteList) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        UserRoleDAO userRoleDao = sqlSession.getMapper(UserRoleDAO.class);
        try {
            int num = userRoleDao.deleteRoleByUserIdAndRoleId(userId, roleIdDeleteList);
            if(num < roleIdDeleteList.size()){
                throw new OurException(UserRoleCodeEnum.USER_ROLE_DELETEEXP_ENUM);
            }
            sqlSession.commit();
        } catch (OurException e) {
            sqlSession.rollback();
            throw e;
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public void addRoleByUserIdAndRoleId(Integer userId, List<String> roleIdAddList) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        UserRoleDAO userRoleDao = sqlSession.getMapper(UserRoleDAO.class);
        try {
            int num = userRoleDao.insertRoleByUserIdAndRoleId(userId,roleIdAddList);
            if(num < roleIdAddList.size()){
                throw new OurException(UserRoleCodeEnum.USER_ROLE_INSERTEXP_ENUM);
            }
            sqlSession.commit();
        } catch (OurException e) {
            sqlSession.rollback();
            throw e;
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public void deleteAllRoleByUserId(Integer userId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        UserRoleDAO userRoleDao = sqlSession.getMapper(UserRoleDAO.class);
        userRoleDao.deleteAllRoleByUserId(userId);
        sqlSession.commit();
        sqlSession.close();
    }
}
